package org.yi.fc.ext.beetl.tag;

import java.io.IOException;
import java.util.List;

import org.beetl.core.GeneralVarTagBinding;
import org.yi.fc.entity.FColumn;
import org.yi.fc.entity.FContentType;

/**
 * 输出栏目信息，用tr包裹(最多支持五级栏目)
 * @author ilgqh
 *
 */
public class ColumnListTag  extends GeneralVarTagBinding{

	/**
	 * 用法：<br/>
	 * 
	 * <br/>
	 * columnNo 栏目编号<br/>
	 */
	@Override
	public void render() {
		//获取栏目编号
		List<FColumn> list = FColumn.dao.getTopNodeColumn();
		
		try {
			for(FColumn fColumn : list){
				echoChildColumn(fColumn, "",false);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				ctx.byteWriter.writeString("<style type='text/css'>.arrow{margin-right:5px;}.horizontal{ -moz-border-bottom-colors: none;-moz-border-left-colors: none;-moz-border-right-colors: none;-moz-border-top-colors: none;border-color: -moz-use-text-color;border-image: none;border-style: dotted;border-width: 0 0 0 1px;height: 30px;}.vertical{border-width: 0px 0px 1px; border-style: dotted; border-color: -moz-use-text-color; -moz-border-top-colors: none; -moz-border-right-colors: none; -moz-border-bottom-colors: none; -moz-border-left-colors: none; border-image: none; height: 1px; width: 10px; margin-right: 15px; }</style>");
			} catch (IOException e) {
				e.printStackTrace();
			}
			this.doBodyRender();
		}
	}
	
	/**
	 * 输出子栏目的相关信息
	 * @param fColumn    父栏目
	 * @throws IOException 
	 */
	private final void echoChildColumn(FColumn fColumn, String tdStyle, boolean isChild) throws IOException{

		echoColumnInfo(fColumn,tdStyle,isChild);
		List<FColumn> list = FColumn.dao.getNextLevelChildrenFColumnsByParentId(fColumn.get("id"));
		for(FColumn column : list){
				if(FColumn.dao.getNextLevelChildrenFColumnsByParentId(fColumn.get("id")).size() > 0){
					tdStyle += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" ;
					echoChildColumn(column, tdStyle, true);
					tdStyle = "&nbsp;&nbsp;";
				}
			}
	}
	
	/**
	 * 输出栏目信息
	 * @param column		栏目实体
	 * @param tdStyle		栏目风格样式
	 * @return
	 * @throws IOException 
	 */
	private final void echoColumnInfo(FColumn column, String tdStyle, boolean isChild) throws IOException{
		StringBuffer sb = new StringBuffer();
		if(isChild){
			tdStyle =  "<label class='horizontal'></label><label class='vertical'></label>" + tdStyle;
		}else{
			tdStyle = "<label class='glyphicon glyphicon-chevron-down green arrow'></label>" + tdStyle;
		}
		sb.append("<tr>")
		//复选框
		.append("<td><input type='checkbox' name='columnNo' class='columnNo' value='").append(column.get("columnNo")).append("'/></td>")
		
		//栏目分类名称
		.append("<td>").append(tdStyle)
		//中文分类名
			.append("<input type='text' name='columnNameCN' value='").append(column.get("columnNameCN")).append("' maxlength=200/>")
			.append("—")
		//英文分类名	
			.append("<input type='text' name='columnNameEN' value='").append(column.get("columnNameEN")).append("' maxlength=200/>")
		.append("</td>")
		
		//栏目所属类型
		.append("<td>").append(FContentType.dao.findById( (Integer)column.get("contentTypeId")).get("typeNameCN")) .append("</td>")
	
		//栏目链接
		.append("<td><a target='_blank' href='").append(column.get("columnLink","")).append("'>").append(column.get("columnLink","")) .append("</a></td>")
		
		//栏目所处状态
		.append("<td>").append(column.get("columnStatus")) .append("</td>")
		
		//操作
		.append("<td style='text-align: center;'>")
			.append("<span>")
				.append("<a href='javascript:void(0)' class='add-childColumn' column-no='").append(column.get("columnNo")).append("'>添加子栏目</a>&nbsp;&nbsp;")
				.append("<a href='javascript:void(0)' class='edit-column' column-no='").append(column.get("columnNo")).append("'>修改</a>")
			.append("</span>")
		.append("</td>")
		
		// end of </tr>
		.append("</tr>")
		;
		
		ctx.byteWriter.writeString(sb.toString());
	}
}
